package ${techspec.packageName}.pages;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ConversationScoped;
import javax.enterprise.context.RequestScoped;
import javax.faces.model.SelectItem;
import javax.inject.Named;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import ${techspec.packageName}.entity.*;
import ${techspec.packageName}.pages.generated.Generated${page.x.javaName};

/**
 * This object represents the ${page.name} page in the application.
 * 
 *  Not sure if this object is really needed or not, but creating it for now 
 *  as a holder of user facing input/output objects.
 *  
 *  This object should also vastly decouple the view from the business logic
 *  
 */
@Named
@RequestScoped
public class ${page.x.javaName} extends Generated${page.x.javaName} {
    private final Log log = LogFactory.getLog(getClass());

## // forms
#foreach ($form in $page.forms)
## // actions
#foreach ($action in $form.actions)
    public String ${action.x.javaId}() {
        log.info("${action.x.javaId} called...");

#if ($action.type == "CREATE")
#foreach($entity in $form.entities)
        // "Create" semantics of "CRUD"
        ${entity.id}Dao.create(${entity.id});
        
## begin -- if there are any output lists on this page for this entity, clear it
#foreach ($outputList in $page.outputListMap.get($entity))
        clear${outputList.name}();
#end
## end -- if there are any output lists on this page for this entity, clear it
## begin - clear form 
## if this form was used to create something, we want the form cleared
## as we don't want to show old values again
        clear${form.name}();
## end - clear form
        return ${action.x.javaId}${action.defaultOutcome.name}();
#end
#elseif ($action.type == "CREATE_OR_UPDATE")
#foreach($entity in $form.entities)
        // "Create or Update" semantics of "CRUD"
        ${entity.id}Dao.createOrUpdate(${entity.id});
#end
        return ${action.x.javaId}${action.defaultOutcome.name}();

#elseif ($action.type == "UPDATE")
#foreach($entity in $form.entities)
        // "Update" semantics of "CRUD"
        ${entity.id}Dao.update(${entity.id});
#end
        return ${action.x.javaId}${action.defaultOutcome.name}();
#elseif ($action.type == "DELETE")
#foreach($entity in $form.entities)
        // "Delete" semantics of "CRUD"
        ${entity.id}Dao.delete(${entity.id});
#end
        return ${action.x.javaId}${action.defaultOutcome.name}();
#elseif ($action.type == "LOGOUT")
        identity.logout();
        return ${action.x.javaId}${action.defaultOutcome.name}();
#else ## // if action.type == create
        // TODO: fill out real implementation here
        log.warn("Method not implemented yet.  Please implement ${className}.${action.id}()");
        return ${action.x.javaId}${action.defaultOutcome.name}();
#end ##if action type is present
    }
#end##// foreach action
#end##// foreach form

## // foreach output list
#foreach ($outputList in $page.outputLists)
    protected List<${techspec.packageName}.entity.${outputList.entity.name}> load${outputList.name}() {
        return ${outputList.entity.id}Service.list${outputList.name}();
    }
#end
}